<template>
  <div
    class="relative h-screen bg-white dark:zinc-800 text-center xl:bg-zinc-200"
  >
    <!-- 图标位置 -->
    <HeaderVue></HeaderVue>
    <!-- 表单 -->
    <div
      class="block px-3 xl:py-4 mt-4 dark:bg-zinc-800 xl:bg-white xl:w-[388px] xl:dark:bg-zinc-900 xl:m-auto xl:mt-8 sl:rounded-sm xl:shadow-lg"
    >
      <h3
        class="hidden mb-2 font-semibold text-base text-main dark:text-zinc-300 xl:block"
      >
        注册账号
      </h3>
      <!-- 表单 -->
      <VeeForm @submit="onRegister">
        <!-- 用户名 -->
        <VeeField
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-0 pb-1 px-1 text-base focus:border-b-main dark:focus:border-v-zinc-200 xl:default:bg-zinc-900"
          type="text"
          name="username"
          placeholder="用户名"
          autocomplete="on"
          :rules="validateUsername"
          v-model="registerForm.username"
        />
        <VeeErrorMessage
          class="text-sm text-red-600 block mt-0.5 text-left"
          name="username"
        />
        <!-- 密码 -->
        <VeeField
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-0 pb-1 px-1 text-base focus:border-b-main dark:focus:border-v-zinc-200 xl:default:bg-zinc-900"
          type="password"
          name="password"
          placeholder="密码"
          autocomplete="on"
          :rules="validatePassword"
          v-model="registerForm.password"
        />
        <VeeErrorMessage
          class="text-sm text-red-600 block mt-0.5 text-left"
          name="password"
        />
        <!-- 确认密码 -->
        <VeeField
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-0 pb-1 px-1 text-base focus:border-b-main dark:focus:border-v-zinc-200 xl:default:bg-zinc-900"
          type="password"
          name="confirmPassword"
          placeholder="确认密码"
          autocomplete="on"
          rules="validateConfirmPassword:@password"
          v-model="registerForm.confirmPassword"
        />
        <!-- 
        rules="validateConfirmPassword:@password" 
         这个写法是将前边的字段关联到后边这个字段 
         -->
        <VeeErrorMessage
          class="text-sm text-red-600 block mt-0.5 text-left"
          name="confirmPassword"
        />
        <!-- 跳转按钮 -->
        <div class="pt-1 pb-3 leading-[0px] text-right">
          <a
            @click="onToLogin"
            class="inline-block p-1 text-zinc-400 text-right dark:text-zinc-600 hover:text-zinc-600 dark:hover:text-zinc-400 text-sm duration-300 cursor-pointer"
          >
            去登录
          </a>
        </div>
        <!-- 注册协议 -->
        <div class="text-center">
          <a
            class="text-zinc-400 dark:text-zinc-600 hover:text-zinc-600 dark:hover:text-zinc-400 text-sm duration-400"
            href="https://m.imooc.com/newfaq?id=89"
            target="__black"
          >
            注册即同意《慕课网注册协议》
          </a>
        </div>

        <!-- 登录按钮 -->
        <m-button
          :loading="loading"
          class="w-full dark:bg-zinc-900 xl:dark:bg-zinc-800"
        >
          立即注册
        </m-button>
      </VeeForm>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { LOGIN_TYPE_USER } from '@/constants'

const router = useRouter()
const route = useRoute()
import {
  Form as VeeForm,
  Field as VeeField,
  ErrorMessage as VeeErrorMessage,
  defineRule
} from 'vee-validate'
import {
  validateUsername,
  validatePassword,
  validateConfirmPassword
} from '../validate.js'
import HeaderVue from '../components/header.vue'
import store from '../../../store/index.js'

/*
 确认密码需要将校验的函数注册shu添加到vee-validate中
 第一个是参数名，第二个是校验函数
 然后将字段关联到确认字段上
 rules="validateConfirmPassword:@password"
 */
defineRule('validateConfirmPassword', validateConfirmPassword)
const loading = ref(false)
const registerForm = reactive({
  username: 'LGD_Sunday',
  password: '123123',
  confirmPassword: ''
})

const onToLogin = () => {
  store.commit('app/changeRouterType', 'push')
  router.push('/login')
}
const onRegister = async () => {
  loading.value = true
  const { username, password } = registerForm
  const params = {
    username,
    password,
    loginType: LOGIN_TYPE_USER
  }
  try {
    const { code, success, message } = await store.dispatch(
      'user/onRegister',
      params,
      ...router.query
    )
    if (code == 200 && success) {
      await store.dispatch('user/onLogin', params)
      router.push('./')
    } else {
      message('error', message)
    }
  } finally {
    loading.value = false
  }
}
</script>
<style scoped lang="scss"></style>
